[pull] main from facebook:main#546
Merged
Merged
Conversation
…ctions (#36500) ## What Codegen registers `_c` (the memo cache import) as a side effect whenever a function compiles with memo slots. The registration persists on `ProgramContext.imports` even if the function is later discarded (`'use no forget'`, `'use no memo'`, lint mode, validation errors). If other applied functions in the file compile to 0 memo slots, the stale `import { c as _c } from "react/compiler-runtime";` leaks into the output. ## Fix In `applyCompiledFunctions`, drop the memo cache import if no applied function uses memo slots. If `react/compiler-runtime` has no remaining specifiers, drop the module entry too so we don't emit a bare `import "react/compiler-runtime";`. ## Reproducer `use-no-forget-multiple-with-eslint-suppression.js`: ```js import {useRef} from 'react'; const useControllableState = options => {}; function NoopComponent() {} function Component() { 'use no forget'; const ref = useRef(null); // eslint-disable-next-line react-hooks/rules-of-hooks ref.current = 'bad'; return <button ref={ref} />; } ``` `NoopComponent` applies with 0 memo slots. `Component` is opted out, but codegen already registered `_c` for it. Before: ```js import { c as _c } from "react/compiler-runtime"; import { useRef } from "react"; ``` After: ```js import { useRef } from "react"; ``` ## Prior art TS counterpart to the Rust port's fix in 7e26eb8. That commit also added `no-cache-slots-no-import.js`, which codifies the "no memo slots, no import" rule. ## Test plan - `yarn snap`: 1719/1719 passing - Snapshot for `use-no-forget-multiple-with-eslint-suppression` loses its spurious `_c` import - `no-cache-slots-no-import` still passes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See Commits and Changes for more details.
Created by
pull[bot] (v2.0.0-alpha.4)
Can you help keep this open source service alive? 💖 Please sponsor : )